//
//  ExercisesAnswerCardCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/10.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "ExercisesAnswerCardItemCell.h"
#import "PaddingLabel.h"
@interface ExercisesAnswerCardItemCell ()
@property (nonatomic,strong)NSIndexPath *indexPath;
@property (nonatomic,strong)ExercieseQuestionItemModel *model;
@property (nonatomic,strong)UIButton *cardBtn;
@property (nonatomic,strong)PaddingLabel *unAnswerLb;
@end

@implementation ExercisesAnswerCardItemCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.cardBtn = [[UIButton alloc]init];
    self.cardBtn.layer.cornerRadius = 19;
    self.cardBtn.layer.masksToBounds = YES;
    self.cardBtn.layer.borderColor = [RPAppManager themColor].CGColor;
    [self.cardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.cardBtn.titleLabel.font = font(14);
    [self.cardBtn addTarget:self action:@selector(cardMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.cardBtn];
    
    [self.cardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(38, 38));
    }];
    
    self.unAnswerLb = [[PaddingLabel alloc]init];
    self.unAnswerLb.text = @"未答";
    self.unAnswerLb.textColor = [UIColor whiteColor];
    self.unAnswerLb.font = font(10);
    self.unAnswerLb.backgroundColor = RGBA(255, 100, 99, 1);
    self.unAnswerLb.layer.borderWidth = 1;
    self.unAnswerLb.layer.borderColor = [UIColor whiteColor].CGColor;
    self.unAnswerLb.layer.cornerRadius = 3;
    self.unAnswerLb.layer.masksToBounds = YES;
    self.unAnswerLb.hidden = YES;
    self.unAnswerLb.edgeInsets = UIEdgeInsetsMake(2, 5, 2, 5);
    [self.contentView addSubview:self.unAnswerLb];
    
    [self.unAnswerLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(19);
        make.top.mas_equalTo(self.contentView).offset(-8);
    }];
}

- (void)setCartItem:(ExercieseQuestionItemModel *)itemModel indexPath:(NSIndexPath *)indexPath{
    self.model = itemModel;
    self.indexPath = indexPath;
    
    if (itemModel.answerItem != nil) {
        if (itemModel.answerItem.doRight) {
               // 判断是否是不计分
               if (itemModel.answerItem.content.length == 0) {
                   self.cardBtn.backgroundColor = RGBA(229, 229, 229, 1);
               }else{
                   self.cardBtn.backgroundColor = RGBA(109, 221, 127, 1);
               }
           }else{
               // 判断是否答题
               if (itemModel.answerItem.content.length == 0) {
                   self.unAnswerLb.hidden = NO;
               }else{
                   self.unAnswerLb.hidden = YES;
               }
               self.cardBtn.backgroundColor = RGBA(255, 100, 99, 1);
           }
    }else{
        if (itemModel.type.intValue == 5) {
            [self.cardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.cardBtn.backgroundColor = RGBA(229, 229, 229, 1);
            self.cardBtn.layer.borderWidth = 0;
        }else{
            __block BOOL isSelect = NO;
            if (itemModel.type.intValue == 1) {
                [itemModel.items enumerateObjectsUsingBlock:^(ExercisesQuestionChoiceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (obj.isSelected) {
                        isSelect = YES;
                        *stop = YES;
                    }
                }];
                // 判断已选对象是否存在并且是否选中
                if (isSelect) {
                    [self.cardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    self.cardBtn.backgroundColor = [RPAppManager themColor];
                }else{
                    self.cardBtn.backgroundColor = [UIColor whiteColor];
                    [self.cardBtn setTitleColor:[RPAppManager themColor] forState:UIControlStateNormal];
                }
                self.cardBtn.layer.borderWidth = isSelect?0:1;
            }
        }
    }
    [self.cardBtn setTitle:[NSString stringWithFormat:@"%lu",itemModel.questionIndex + 1] forState:UIControlStateNormal];
}
- (void)cardMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedAtIndexPath:)]) {
        [self.delegate didSelectedAtIndexPath:self.indexPath];
    }
}

@end
